<?php 
namespace Common\Model;
use Think\Model;
class ArticleModel extends Model{

	protected $table;
	protected $pre;
	protected $arttable;

	public function __construct($arttable,$pre = ''){
		parent::__construct();
		$this->pre = empty($pre) ? C('DB_PREFIX') : $pre;
		$this->table = $arttable;
		$this->arttable = $this->pre.$arttable;  
	}

	public function addArt($data){
		$check = $this -> checkInfo($data);
		if($check !== true){
			return $check;
		}
		$artData = $this -> formatInfo($data);
		$article = M($this->table);
		$mid = $article -> add($artData,[],true);
		if(!$mid){
			return $this -> returnInfo('','失败');
		}
		return $this -> returnInfo('','成功了！',1);
	}

	public function checkInfo($data){
		if(empty($data['title'])){
			return $this -> returnInfo('','文章标题不能为空！');
		}
		if(strlen($data['sort']) > 8){
			return $this -> returnInfo('','排序不符合规则');
		}
		if(empty($data['id'])){
			$artCont = M($this->table) -> field('id') -> where(['title' => $data['title']]) -> count();
			if($artCont > 0){
				return $this -> returnInfo('','文章已存在');
			}
		}
		return true; 
	}

	public function formatInfo($data){
		$id = empty($data['id']) ? 0 : $data['id'];
		$cat_id = empty($data['cat_id']) ? 0 : $data['cat_id'];
		$tag = empty($data['tag']) ? '' : $data['tag'];
		$imgPath = empty($data['litpic']) ? 0 : $data['litpic'];
		$author = empty($data['author']) ? '' : $data['author'];
		$content = empty($data['content']) ? '' : $data['content'];
		$seo_title = empty($data['seo_title']) ? '' : $data['seo_title'];
		$seo_keywords = empty($data['seo_keywords']) ? '' : $data['seo_keywords'];
		$seo_description = empty($data['seo_description']) ? '' : $data['seo_description'];
		$is_html = empty($data['is_html']) ? 1 : $data['is_html'];
		$hits = empty($data['hits']) ? 0 : $data['hits'];
		$sort = empty($data['sort']) ? 0 : $data['sort'];
		$addman = session('user.account');	
		$dataInfo = ['id' => $id,'cat_id' => $cat_id,'tag' => $tag,'title' => $data['title'],'img' => $imgPath,'author' => $author,'content' => $content , 'seo_title' => $seo_title ,'seo_keywords' => $seo_keywords,'seo_description' => $seo_description,'is_html' => $is_html,'hits' => $hits ,'sort' => $sort,'addman' => $addman];
		// dump($dataInfo);exit;
		return $dataInfo;

	}

	public function artlist($id,$field = 'id',$alias,$join,$sql,$frow,$num,$order = 'id asc'){
		$article = M($this->table);
		$list = $article -> field($field);
		$list = $list -> alias($alias) -> join($join);
		if(!empty($id)){
			$list = $list -> where(['id' => $id]); 
		}else{
			$list = $list -> where($sql);
		}		
		$list = $list -> limit($frow,$num);
		$list = $list -> order($order) -> select();
		if(empty($list)){
			return $this -> returnInfo('','没有数据');
		}else{
			return $this -> returnInfo($list,'成功',1);
		}
	}

	public function returnInfo($data,$msg = '',$status = 0){
		return ['status' => $status , 'mess' => $msg ,'data' => $data];
	}

	public function Delart($id){
		if(empty($id)){
			return $this -> returnInfo('','非法操作');
		}
		$is_del = M($this->table) -> where(['id' => $id]) -> delete(); 
		if(!$is_del){
			return $this -> returnInfo('','删除失败');
		}
		return $this -> returnInfo('','删除成功',1);
	}


}


